package com.yunjian.core.basic.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yunjian.core.basic.mapper.SRoleMapper;
import com.yunjian.core.basic.model.SRole;
import com.yunjian.core.basic.model.SRoleExample;
import com.yunjian.core.basic.model.SRoleResourceExample;
import com.yunjian.core.basic.model.SUserRoleExample;
import com.yunjian.core.basic.service.SRoleResourceService;
import com.yunjian.core.basic.service.SRoleService;
import com.yunjian.core.basic.service.SUserRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SRoleServiceImpl implements SRoleService{
	
	@Autowired
	private SRoleMapper mapper;
	
	@Autowired
	private SUserRoleService sUserRoleService;
	@Autowired
	private SRoleResourceService sRoleResourceService;

	@Override
	public int countByExample(SRoleExample example) {
		return mapper.countByExample(example);
	}

	@Override
	public int deleteByExample(SRoleExample example) {
		return mapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(String userId) {
		return mapper.deleteByPrimaryKey(userId);
	}

	@Override
	public int insert(SRole record) {
		return mapper.insert(record);
	}

	@Override
	public int insertSelective(SRole record) {
		return mapper.insertSelective(record);
	}

	@Override
	public List<SRole> selectByExample(SRoleExample example) {
		return mapper.selectByExample(example);
	}

	@Override
	public SRole selectByPrimaryKey(String userId) {
		return mapper.selectByPrimaryKey(userId);
	}

	@Override
	public int updateByExampleSelective(SRole record, SRoleExample example) {
		return mapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(SRole record, SRoleExample example) {
		return mapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(SRole record) {
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(SRole record) {
		return mapper.updateByPrimaryKey(record);
	}

	@Override
	public PageInfo<SRole> selectPagedByExample(int page, int rows,
			SRoleExample example) {
		PageHelper.startPage(page, rows);
		PageInfo<SRole> pageInfo = new PageInfo<>(mapper.selectByExample(example));
		return pageInfo;
	}

	@Override
	public void deleteRoleRelativeByPrimaryKey(String key) {
		// 删除用户角色
		SUserRoleExample example = new SUserRoleExample();
		example.createCriteria().andRoleIdEqualTo(key);
		sUserRoleService.deleteByExample(example);
		// 删除角色分配的资源
		SRoleResourceExample rrEx = new SRoleResourceExample();
		rrEx.createCriteria().andRoleIdEqualTo(key);
		sRoleResourceService.deleteByExample(rrEx);
		// 删除角色
		mapper.deleteByPrimaryKey(key);
	}
	
}
